package com.jieli.stream.dv.running2.util;

import com.jieli.stream.dv.running2.ui.activity.MainActivity;
import com.jieli.stream.dv.running2.ui.base.BaseActivity;

import java.util.Stack;

public final class ActivityManager {
    private Stack<BaseActivity> activityStack;
    private static ActivityManager instance = null;

    public static ActivityManager getInstance() {

        if (instance == null) {
            synchronized (ClientManager.class) {
                if (instance == null) {
                    instance = new ActivityManager();
                }
            }
        }
        return instance;
    }

    public void pushActivity(BaseActivity baseActivity) {
        if (activityStack == null)
            activityStack = new Stack<>();
        activityStack.add(baseActivity);
        //  Dbug.e("activityStack", "add activity = " + baseActivity.toString());
    }

    public void popActivity(BaseActivity baseActivity) {
        if (activityStack != null)
            activityStack.remove(baseActivity);
        // Dbug.e("activityStack", "remove activity = " + baseActivity.toString());
    }

    public BaseActivity getTopActivity() {
        if (activityStack == null) return null;

        return activityStack.lastElement();
    }

    public void popAllActivity() {
        if (activityStack == null) return ;

        for (BaseActivity activity : activityStack) {
            if (activity != null) {
                activity.finish();
            }
        }
        activityStack.clear();
    }

    public void popActivityOnlyMain() {
        if (activityStack == null) return ;

        for (BaseActivity activity : activityStack) {
            if (activity != null && !(activity instanceof MainActivity)) {
                activity.finish();
            }
        }
    }
}
